package com.itheima.info.manager.dao;

import com.itheima.info.manager.domain.Teacher;

import java.util.ArrayList;

public class TeacherDao implements BaseTeacherDao {

    private static ArrayList<Teacher> teacherArrayList = new ArrayList<>();
    static {
        Teacher teacher = new Teacher("0001","王五",23,"1991-1-1");
        Teacher teacher2 = new Teacher("0002","赵六",28,"1993-1-1");
        Teacher teacher3 = new Teacher("0003","李四",21,"1994-1-1");
        teacherArrayList.add(teacher);
        teacherArrayList.add(teacher2);
        teacherArrayList.add(teacher3);
    }
    //添加老师
    @Override
    public void addTeacher(Teacher teacher) {
        teacherArrayList.add(teacher);
    }
    //删除老师
    @Override
    public void deleteTeacher(String id) {
        int i = getIndex(id);
        teacherArrayList.remove(i);
    }
    //查看老师
    @Override
    public ArrayList findAllTeacher() {
        return teacherArrayList;
    }
    //修改老师
    @Override
    public void updateTeacher(String id, Teacher teacher) {
        int i = getIndex(id);
        teacherArrayList.set(i,teacher);
    }
    //根据id查索引
    @Override
    public int getIndex(String id) {
        Teacher teacher = new Teacher();
        int index = -1;
        for (int i = 0; i < teacherArrayList.size(); i++) {
            teacher = teacherArrayList.get(i);
            if (teacher != null && teacher.getId().equals(id)){
                index = i;
            }
        }
        return index;
    }
}
